"""
-*- coding: utf-8 -*-
@Author  : Link
@Time    : 2022/5/9 17:44
@Software: PyCharm
@File    : ui_tree_load_widget.py
@Remark  :
"""
from PySide2.QtCore import Slot, Qt
from PySide2.QtWidgets import QWidget, QTreeWidgetItem, QInputDialog

from ui_component.ui_to_analysis_stdf.ui_designer.ui_tree_load import Ui_Form as TreeLoadForm
from ui_component.ui_to_analysis_stdf.utils import TreeUtils


class TreeLoadWidget(QWidget, TreeLoadForm):
    """
    DataTree & Limit List
    """
    parent = None

    def __init__(self, parent=None):
        super(TreeLoadWidget, self).__init__(parent)
        self.parent = parent
        self.setupUi(self)
        self.setWindowTitle("Data Tree Select & Config")

    @Slot(QTreeWidgetItem)
    def on_treeWidget_itemChanged(self, e: QTreeWidgetItem):
        TreeUtils.tree_item_change(self.treeWidget, e)

    @Slot()
    def on_pushButton_pressed(self):
        """
        calculation df
        """
        if self.parent.summary.summary_df is None:
            return self.parent.message_show("未载入数据到数据空间!")
        self.parent.li.set_data(self.parent.summary.get_df(
            TreeUtils.get_tree_ids(self.treeWidget), self.checkBox.checkState(), self.spinBox.value()
        ))
        self.parent.li.set_limit_dict(self.parent.summary.limit_dict)
        self.parent.li.calculation_cpk()

    @Slot()
    def on_pushButton_2_pressed(self):
        """
        Merge多份数据成为一份自定义的数据
        """
        ids = TreeUtils.get_tree_ids(self.treeWidget)
        if not ids:
            return self.parent.message_show("未载入数据到数据空间, 无法解析!")
        remark, _ = QInputDialog.getText(self, "输入自定义LOT_ID", "请填写自定义的LOT_ID用于整合多个LOT成为一个数据;自定义输入的长度不能<2!")
        remark = remark.replace(" ", "").upper()
        if len(remark) < 2:
            return self.parent.message_show("自定义输入的长度不能<2!")
        self.parent.summary.add_custom_node(ids, remark)
        self.parent.tree_data_load()


if __name__ == '__main__':
    import sys
    from PySide2.QtWidgets import QMainWindow, QApplication

    app = QApplication(sys.argv)
    win = TreeLoadWidget()
    win.show()
    sys.exit(app.exec_())
